local jiqiao = fk.CreateSkill {

  name = "joy__jiqiao",

  tags = {  },

}



jiqiao:addEffect(fk.EventPhaseStart, {
  name = "joy__jiqiao",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jiqiao.name) and player.phase == Player.Play and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local n = #player.room:askForDiscard(player, 1, 999, true, jiqiao.name, true, ".|.|.|.|.|^trick", "#joy__jiqiao-invoke")
    if n > 0 then
      event:setCostData(self, n)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(2*event:getCostData(self))
    room:moveCards{
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      skillName = jiqiao.name,
    }
    local get = {}
    for i = #cards, 1, -1 do
      if Fk:getCardById(cards[i]).type == Card.TypeTrick then
        table.insert(get, cards[i])
        table.removeOne(cards, cards[i])
      end
    end
    if #get > 0 then
      room:delay(1000)
      local dummy = Fk:cloneCard("dilu")
      dummy:addSubcards(get)
      room:obtainCard(player, dummy, true, fk.ReasonJustMove)
    else
      local cards = room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #cards > 0 and not player.dead then
        local get = cards[1]
        room:obtainCard(player, get, false, fk.ReasonDraw)
      end
    end
    if #cards > 0 then
      room:delay(1000)
      room:moveCards{
        ids = cards,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonJustMove,
        skillName = jiqiao.name,
      }
    end
  end,
})

return jiqiao